package com.ede.roverhu.simplebackstage.UI;

/**
 * Created by Admin on 2017/6/18.
 */

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.ede.roverhu.simplebackstage.Constant.HandlerConstant;
import com.ede.roverhu.simplebackstage.Domain.QueryDomain;
import com.ede.roverhu.simplebackstage.Domain.SchoolBase;
import com.ede.roverhu.simplebackstage.Domain.xueyuanDomain;
import com.ede.roverhu.simplebackstage.Http.HttpClient;
import com.ede.roverhu.simplebackstage.R;
import com.ede.roverhu.simplebackstage.Utils.LogUtils;
import com.ede.roverhu.simplebackstage.Utils.ToastUtils;

import java.util.ArrayList;

/**
 * 创建自定义的dialog，主要学习其实现原理
 * Created by chengguo on 2016/3/22.
 */
public class SelfDialog extends Dialog {
    private Button yes;//确定按钮
    private Button no;//取消按钮
    private TextView titleTv;//消息标题文本
    private TextView messageTv;//消息提示文本
    private String titleStr;//从外界设置的title文本
    private String messageStr;//从外界设置的消息文本
    private String EditTextHint;
    //确定文本和取消文本的显示内容
    private String yesStr, noStr;
    private ArrayList<SchoolBase> schoolList;
    private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器

    private QueryDomain queryDomain;

    private Spinner s_school;
    private Spinner s_xueyuan;
    private Spinner s_xi;
    private Spinner s_zhuanye;
    private Spinner s_class;

    private EditText editText;

    private SharedPreferences sharedPreferences;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case HandlerConstant.QUERYFID_SUCCESS:
                    s_xueyuan.setVisibility(View.VISIBLE);
                    final ArrayList<xueyuanDomain> xylist = (ArrayList<xueyuanDomain>) msg.obj;
                    if (xylist == null)
                        return;
                    String[] xyArr = new String[xylist.size()];
                    for (int z = 0; z < xylist.size(); z++) {
                        xyArr[z] = xylist.get(z).ar_title;
                    }
                    s_xueyuan.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, xyArr));
                    s_xueyuan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            queryDomain.fid = xylist.get(i).ar_id;
                            HttpClient.querybid(queryDomain, handler);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                    break;
                case HandlerConstant.QUERYFID_FAIL:
                    ToastUtils.show(getOwnerActivity(), "学院列表获取失败");
                    break;

                case HandlerConstant.QUERYMID_SUCCESS:
                    s_zhuanye.setVisibility(View.VISIBLE);
                    final ArrayList<xueyuanDomain> zylist = (ArrayList<xueyuanDomain>) msg.obj;
                    if (zylist == null)
                        return;
                    String[] zyArr = new String[zylist.size()];
                    for (int z = 0; z < zylist.size(); z++) {
                        zyArr[z] = zylist.get(z).ar_title;
                    }
                    s_zhuanye.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, zyArr));
                    s_zhuanye.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            queryDomain.mid = zylist.get(i).ar_id;
                            HttpClient.querycid(queryDomain, handler);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                    break;
                case HandlerConstant.QUERYMID_FAIL:
                    ToastUtils.show(getOwnerActivity(), "专业列表获取失败");
                    break;

                case HandlerConstant.QUERYBID_SUCCESS:
                    s_xi.setVisibility(View.VISIBLE);
                    final ArrayList<xueyuanDomain> xilist = (ArrayList<xueyuanDomain>) msg.obj;
                    if (xilist == null)
                        return;
                    String[] xiArr = new String[xilist.size()];
                    for (int z = 0; z < xilist.size(); z++) {
                        xiArr[z] = xilist.get(z).ar_title;
                    }
                    s_xi.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, xiArr));
                    s_xi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            queryDomain.bid = xilist.get(i).ar_id;
                            HttpClient.querymid(queryDomain, handler);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                    break;
                case HandlerConstant.QUERYBID_FAIL:
                    ToastUtils.show(getOwnerActivity(), "系列表获取失败");
                    break;

                case HandlerConstant.QUERYCID_SUCCESS:
                    s_class.setVisibility(View.VISIBLE);
                    final ArrayList<xueyuanDomain> clist = (ArrayList<xueyuanDomain>) msg.obj;
                    if (clist == null)
                        return;
                    String[] cArr = new String[clist.size()];
                    for (int z = 0; z < clist.size(); z++) {
                        cArr[z] = clist.get(z).ar_title;
                    }
                    s_class.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, cArr));
                    s_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                            queryDomain.cid = clist.get(i).ar_id;
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> adapterView) {

                        }
                    });
                    break;
                case HandlerConstant.QUERYCID_FAIL:
                    ToastUtils.show(getOwnerActivity(), "班级列表获取失败");
                    break;

            }

        }
    };

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param str
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
        if (str != null) {
            noStr = str;
        }
        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param str
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
        if (str != null) {
            yesStr = str;
        }

        this.yesOnclickListener = onYesOnclickListener;
    }

    public SelfDialog(Context context) {
        super(context, R.style.MyDialog);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_exercise_sure_dialog_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        sharedPreferences = getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null)
                    yesOnclickListener.onYesClick(queryDomain);
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        //如果用户自定了title和message
        if (titleStr != null) {
            titleTv.setText(titleStr);
        }
        if (messageStr != null) {
            messageTv.setText(messageStr);
        }
        //如果设置按钮的文字
        if (yesStr != null) {
            yes.setText(yesStr);
        }
        if (noStr != null) {
            no.setText(noStr);
        }

        if (schoolList != null) {
            LogUtils.e("schoolist");
            queryDomain = new QueryDomain();
            queryDomain.edit = editText;
            final String[] mItems = new String[schoolList.size()];
            for (int z = 0; z < schoolList.size(); z++) {
                mItems[z] = schoolList.get(z).name;
            }
            s_school.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, mItems));
            s_school.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    if (!schoolList.get(i).name.contains("请选择")) {
                        queryDomain.sid = schoolList.get(i).sid;
                        HttpClient.queryfid(queryDomain, handler);
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        } else {
            s_school.setVisibility(View.GONE);
            editText.setHint(EditTextHint);
            LogUtils.e("!schoolist");
            queryDomain = new QueryDomain();
            queryDomain.edit = editText;
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        titleTv = (TextView) findViewById(R.id.title);
        messageTv = (TextView) findViewById(R.id.message);
        editText = (EditText) findViewById(R.id.edit);
        s_school = (Spinner) findViewById(R.id.s_school);
        s_xueyuan = (Spinner) findViewById(R.id.s_xueyuan);
        s_xueyuan.setVisibility(View.GONE);
        s_xi = (Spinner) findViewById(R.id.s_xi);
        s_xi.setVisibility(View.GONE);
        s_zhuanye = (Spinner) findViewById(R.id.s_zhuanye);
        s_zhuanye.setVisibility(View.GONE);
        s_class = (Spinner) findViewById(R.id.s_class);
        s_class.setVisibility(View.GONE);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param message
     */
    public void setMessage(String message) {
        messageStr = message;
    }


    public void setSchoolArr(ArrayList<SchoolBase> schoolList) {
        this.schoolList = schoolList;
    }


    public void setEditTextHint(String message) {
        EditTextHint = message;
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface onYesOnclickListener {
        public void onYesClick(QueryDomain queryDomain);
    }

    public interface onNoOnclickListener {
        public void onNoClick();
    }
}
